package com.wejoy.video.middle.station.admin.controller.system;

import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.system.SysRole;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.service.system.ISysRoleService;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统--角色表
 *
 * @author admin
 * @date 2021年10月19日 15:06:47
 */
@RestController
@RequestMapping("/system/sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 保存或更新
     *
     * @param sysRoleReq sysRoleReq
     * @return RespJSON
     */
    @PostMapping("/save")
    @RoleCheck(name = "sys_role_add,sys_role_edit")
    public RespJSON save(@RequestBody SysRole sysRoleReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        sysRoleService.save(sysRoleReq, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param sysRoleReq sysRoleReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "sys_role_list")
    public RespJSON getPageList(@RequestBody SysRole sysRoleReq) {
        sysRoleReq.setStatus(EnumStatus.NEW.getCode());
        int count = sysRoleService.getCount(sysRoleReq);
        List<SysRole> list = new ArrayList<>();
        if (count > 0) {
            list = sysRoleService.getPageList(sysRoleReq);
        }
        PageResult result = new PageResult(sysRoleReq.getPageNo(), sysRoleReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }


    /**
     * 删除角色
     *
     * @param id
     * @param loginSysUser
     * @return
     */
    @PostMapping("/delete")
    @RoleCheck(name = "sys_role_delete")
    public RespJSON delete(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        sysRoleService.delete(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {

        return RespJSON.success(sysRoleService.getOneById(id));
    }

    /**
     * 获取用户角色
     *
     * @param loginSysUser
     * @return
     */
    @PostMapping("/getCurrentUserRole")
    public RespJSON getCurrentUserRole(@SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        List<SysRole> sysRoleList = sysRoleService.getCurrentUserRole(loginSysUser.getId());
        return RespJSON.success(sysRoleList);
    }


}
